package logic

import (
	"context"
	"net/http"
	"week2_zg6/bookRpc/book"

	"week2_zg6/bookApi/internal/svc"
	"week2_zg6/bookApi/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderLogic {
	return &OrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *OrderLogic) Order(req *types.OrderRequest) (resp *types.OrderResponse, err error) {
	res, err := l.svcCtx.Book.Order(context.Background(), &book.OrderRequest{
		Price:   req.Price,
		Num:     req.Num,
		GoodsID: req.GoodsID,
		UserID:  req.UserID,
		Address: req.Address,
	})
	if err != nil {
		return nil, err
	}
	return &types.OrderResponse{
		Code: http.StatusOK,
		Msg:  "",
		Date: map[string]interface{}{
			"data": res,
		},
	}, nil
}
